package jsyf.oa.service.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.system.UserAchievementsDao;
import jsyf.oa.entity.system.BigAchievementsEntity;
import jsyf.oa.facade.system.UserAchievementsFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 92852 on 2018/8/2.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class UserAchievementsService implements UserAchievementsFacade {
    @Autowired
    private UserAchievementsDao userAchievementsDao;

    @Override
    public JSONObject getListByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        StringBuffer condition = new StringBuffer();
        condition.append("");
        List<Object> sqlArgs = new ArrayList<Object>();
        List<JSONObject> records = userAchievementsDao.getJSONListByCondition(condition.toString(), sqlArgs.toArray());
        // 子数组对象
        JSONObject result = new JSONObject();
        result.put("records", records);
        return result;
    }

    @Override
    public JSONObject getDetailById(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String id = jsonParams.getString("id");
        JSONObject detail = userAchievementsDao.getJSONById(id);
        return detail;
    }

    @Override
    public List<JSONObject> getAchievementByCompany(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        if (StringUtils.isNotEmpty(companyId)) {
            return userAchievementsDao.getAchievementByCompany(companyId);
        }
        return null;
    }

    @Override
    public String save(String jsonData) {
        BigAchievementsEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), BigAchievementsEntity.class);
        if ("0".equals(entity.getId()) || com.alibaba.dubbo.common.utils.StringUtils.isBlank(entity.getId())) {
            userAchievementsDao.add(entity);
        } else {
            userAchievementsDao.update(entity);
        }
        return entity.getId();
    }

    @Override
    public JSONObject delById(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String id = jsonParams.getString("id");
        Integer row = userAchievementsDao.delById(id);
        JSONObject obj = new JSONObject();
        if (row > 0) {
            obj.put("records", "删除成功");
        } else {
            obj.put("records", "删除失败");
        }
        return obj;
    }

    @Override
    public JSONObject removeById(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String id = jsonParams.getString("id");
        Integer row = userAchievementsDao.removeById(id);
        JSONObject obj = new JSONObject();
        if (row > 0) {
            obj.put("records", "删除成功");
        } else {
            obj.put("records", "删除失败");
        }
        return obj;
    }

}
